#!/bin/bash
set -e
if [[ $# -ne 3 ]]; then
    echo "The format is as follows:"
    echo "./Rosetta-3.13.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    echo ":param srcDir: 商业软件包存放目录绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then echo "请输入正确的构建路径"; exit 1; fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then echo "请输入正确的安装路径"; exit 1; fi
[[ "$1" = "$2" ]] && echo "构建路径与安装路径不能相同" && exit 1
buildpath=$1
installpath=$2
srcDir=$3
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi

basepath=$(cd `dirname $0`; pwd)
cd $installpath
tar xvf $srcDir/rosetta_src_3.13_bundle.tgz && mv rosetta_src_2021.16.61629_bundle Rosetta-3.13
cd Rosetta-3.13/main/source
cp -r $basepath/basic.settings.patch ./
cp -r $basepath/setup_platforms.py.patch ./
patch -p0 < basic.settings.patch
patch -p0 < setup_platforms.py.patch
sed -i '324d' external/libxml2/config.h
./scons.py -j $(nproc) mode=release cxx=clang
./scons.py -j $(nproc) mode=release bin cxx=clang
cat>"$installpath/Rosetta-3.13/Rosetta_modulefiles"<<EOF
#%Module1.0
conflict Rosetta
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set Rosetta \$pwd
setenv Rosetta \$Rosetta
prepend-path PATH \$Rosetta/main/source/bin
prepend-path LD_LIBRARY_PATH \$Rosetta/main/source/build/src/release/linux/4.19/64/64/clang/Compiler/default/
prepend-path LD_LIBRARY_PATH \$Rosetta/main/source/build/external/release/linux/4.19/64/64/clang/Compiler/default/
EOF
